package design._01_factory.simple1._02;

import java.io.File;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public abstract class XMLUtil {
	//该方法用于从XML配置文件中提取图表类型，并返回类型名
	public static String getChartType() {
		try {
			//创建文档对象
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = dFactory.newDocumentBuilder();
			Document doc;
			//doc = builder.parse(new File("config.xml"));
			InputStream is= XMLUtil.class.getResourceAsStream("config.xml");
			doc = builder.parse(is);
			
			//获取包含图表类型的文本节点
			NodeList nl = doc.getElementsByTagName("chartType");
			Node classNode = nl.item(0).getFirstChild();
			String chartType = classNode.getNodeValue().trim();
			return chartType;
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
